#include "sessiondetailwidget.h"
#include "debug.h"

AvatarItem::AvatarItem(const QIcon &avatar, const QString &name)
{
    // 1. 设置自身的基本属性
    this->setFixedSize(70, 80);

    // 2. 创建布局管理器
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignHCenter);
    this->setLayout(layout);

    // 3. 创建对象
    _avatarBtn = new QPushButton();
    _avatarBtn->setFixedSize(45, 45);
    _avatarBtn->setIconSize(QSize(45, 45));
    _avatarBtn->setIcon(avatar);
    _avatarBtn->setStyleSheet("QPushButton { border: none; }");

    // 4. 创建名字
    _nameLabel = new QLabel();
    _nameLabel->setText(name);
    QFont font("微软雅黑", 12);
    _nameLabel->setFont(font);
    _nameLabel->setAlignment(Qt::AlignCenter);

    // 5. 对昵称进行截断显示
    const int MAX_WIDTH = 65;
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(name);
    if (totalWidth >= MAX_WIDTH)
    {
        // 进行截断操作
        QString tail       = "...";
        int tailWidth      = metrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        // 计算除去 "..." 昵称还有 % 多少的字符可用
        int availableSize = name.size() * ((double) availableWidth / totalWidth);
        QString newName   = name.left(availableSize);
        _nameLabel->setText(newName + tail);
    }

    // 6. 添加到布局管理器中
    layout->addWidget(_avatarBtn);
    layout->addWidget(_nameLabel);
}

SessionDetailWidget::SessionDetailWidget(QWidget *parent, const model::UserInfo &userInfo)
{
    // 1. 设置基本属性
    this->setWindowTitle("会话详细");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(300, 300);
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(50, 0, 50, 0);
    this->setLayout(layout);

    // 3,添加 “创建群聊” 按钮
    AvatarItem *createGroupBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    layout->addWidget(createGroupBtn, 0, 0);

    // 4.添加当前用户的信息进行测试
#if TEST_UI
    AvatarItem *currentUser = new AvatarItem(QIcon(":/resource/image/defaultAvatar.png"), "张三123456");
    layout->addWidget(currentUser, 0, 1);
#endif
    // AvatarItem* currentUser = new AvatarItem(userInfo.avatar, userInfo.nickname);
    // layout->addWidget(currentUser, 0, 1);

    // 5. 添加 “删除好友” 按钮
    _deleteFriendBtn = new QPushButton();
    _deleteFriendBtn->setFixedHeight(50);
    _deleteFriendBtn->setText("删除好友");
    QString style = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; } ";
    style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    _deleteFriendBtn->setStyleSheet(style);
    layout->addWidget(_deleteFriendBtn, 1, 0, 1, 3);

    // 6. 添加信号槽，处理点击 “创建群聊” 按钮
    connect(createGroupBtn->getAvatar(), &QPushButton::clicked, this,
            [=]()
            {
                ChooseFriendDialog *chooseFriendDialog = new ChooseFriendDialog(this, userInfo.userId);
                chooseFriendDialog->exec();
            });
}
